package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.Permission;
import com.itheima.factory.MapperFactory;
import com.itheima.mapper.PermissionMapper;
import com.itheima.service.PermissionService;
import com.itheima.utils.TransactionUtil;
import com.itheima.utils.Uuid;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class PermissionServiceImpl implements PermissionService {
    
    @Override
    public List<Permission> findAll(String pageNum) {
        SqlSession session = MapperFactory.getSqlSession();

        PermissionMapper mapper = session.getMapper(PermissionMapper.class);
        //设置当前页,默认为第一页
        int pageNumber = 1;
        if (StringUtils.isNotBlank(pageNum)) {
            pageNumber = Integer.parseInt(pageNum);
        }
        // 每页显示条数
        int pageSize = 10;
        //设置分页参数
        PageHelper.startPage(pageNumber, pageSize);
        List<Permission> list = mapper.findAll();

        //释放资源
        TransactionUtil.commit(session);
        TransactionUtil.close(session);

        //返回集合
        return list;
    }


    //修改功能
    @Override
    public boolean edit(Permission permission) {
        SqlSession session = MapperFactory.getSqlSession();

        PermissionMapper mapper = session.getMapper(PermissionMapper.class);
        boolean flag = false;
        try {
            mapper.edit(permission);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放资源
            TransactionUtil.commit(session);
            TransactionUtil.close(session);
        }
        return flag;
    }

    //根据id删除
    @Override
    public boolean deleteByIds(String[] id) {
        SqlSession session = MapperFactory.getSqlSession();

        PermissionMapper mapper = session.getMapper(PermissionMapper.class);
        boolean flag = false;
        try {
            mapper.delete(id);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放资源
            TransactionUtil.commit(session);
            TransactionUtil.close(session);
        }
        return flag;
    }

    //根据id查询
    @Override
    public Permission findById(String id) {
        SqlSession session = MapperFactory.getSqlSession();

        PermissionMapper mapper = session.getMapper(PermissionMapper.class);

        Permission permission = mapper.findById(id);
        //释放资源
        TransactionUtil.commit(session);
        TransactionUtil.close(session);
        return permission;
    }

    /**
     * @param
     * @return java.util.List<com.itheima.domain.store.Permission>
     * @author JANME
     * @date 2020-08-18 15:39
     */
    @Override
    public List<Permission> list() {
        SqlSession session = MapperFactory.getSqlSession();

        PermissionMapper mapper = session.getMapper(PermissionMapper.class);

        List<Permission> list = mapper.findAll();

        //释放资源
        TransactionUtil.commit(session);
        TransactionUtil.close(session);

        return list;
    }

    //添加权限信息信息功能
    @Override
    public boolean add(Permission permission) {
        SqlSession session = MapperFactory.getSqlSession();

        PermissionMapper mapper = session.getMapper(PermissionMapper.class);
        //定义标记
        boolean flag = false;
        try {
            //通过实现类调用方法,添加权限信息信息
            String id = Uuid.getId();
            permission.setId(id);
            mapper.add(permission);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放资源
            TransactionUtil.commit(session);
            TransactionUtil.close(session);
        }
        return flag;
    }

    @Override
    public List<Permission> findByRole(String roleId) {
        // 获取SqlSession对象
        SqlSession session = MapperFactory.getSqlSession();
        // 获取接口动态实现对象
        PermissionMapper mapper = MapperFactory.getMapper(session,PermissionMapper.class);
        // 操作数据库
        List<Permission> list = mapper.findByRole(roleId);
        TransactionUtil.commit(session);
        TransactionUtil.close(session);
        return list;
    }
}
